/////////////////////////////////////////////////////////////////////////////
/*/	
 *	Creator:		Joshua C. Rouzer
 *	Title:			Rocketeer cpp
 *	Last Edited:	06/14/2013
 *	Summary:		Behavioral Initialization of the Rocket unit
/*/
/////////////////////////////////////////////////////////////////////////////
#include "Rocketeer.h"
#include "DefenseStruct.h"
#include "LevelManager.h"
#include "CSGD_TextureManager.h"
#include <queue>
/////////////////////////////////////////////////////////////////////////////
// Function : Default Constructor
// Notes	: Gives the unit base traits
/////////////////////////////////////////////////////////////////////////////
CRocketeer::CRocketeer(){
	m_eType = ROCKET;
	SetHealth(100);
	SetMaxHealth(100);
	SetAttackCool(2);
	SetSpecAttCool(25);
	SetBaseDmg(15);
	SetSpecialDmg(25);
	SetAttackWait(3);
	SetSAttackWait(25);
	SetRange(100);
	SetImage( CSGD_TextureManager::GetInstance()->LoadTexture( _T("resource/Images/Rocketeer.png"), D3DCOLOR_ARGB(255,255,255,255) ) );
	SetWidth(64);
	SetHeight(64);
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Destructor
// Notes		: Cleans up any dynamically allocated memory
/////////////////////////////////////////////////////////////////////////////
CRocketeer::~CRocketeer()
{
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Update
// Parameters	: dT - Time elapsed since last update
// Notes		: Handles movement and behavior of the unit
/////////////////////////////////////////////////////////////////////////////
void CRocketeer::Update(float dT){
	//Use this for enemy queue setup
	std::priority_queue<CBaseUnit*> enemyQ;
//	CLevelManager::GetCurrLevel();
	//Behavior
		//Check for Enemies in range
		if(GetTarget() == NULL){
		//YES
			//If so make sure they're in the same path (as clarity)
			//Add all in range enemies to queue
			//Access first target in queue
			//LOOP
			std::vector<CBaseUnit*> swapVec = CBaseUnit::GetEnemiesInRange();

			for(unsigned int i = 0; i < swapVec.size();i++)
				enemyQ.push(swapVec[i]);

			for(unsigned int qIter = 0; qIter < enemyQ.size(); ){
				CBaseUnit *_enemy = enemyQ.top();

				if(_enemy->GetType() == SHIELD)
				{
				//YES
					//Break loop
					//Attack it
					SetTarget(_enemy);
					break;
				}

				//Target's Health < 10%?
				else if(_enemy->GetHealth() <= _enemy->GetMaxHealth() * 0.1f){
				//YES
					//Break loop
					//Attack it
					SetTarget(_enemy);
					break;
				}
				else{
				//NO
					//is it a type you are strong against?
					if(_enemy->GetType() == MECH){
					//YES
						//Break loop
						//Attack it
						SetTarget(_enemy);
						break;
					}
					else{
					//NO
						//Is it the last unit in queue?
						if(enemyQ.size() == 1){
						//YES
							//Break loop
							//Attack it
							SetTarget(_enemy);
							break;
						}
						else{
						//NO
							//Access next target in queue
							enemyQ.pop();
							//Restart loop
						}
					}
				}
			}
		}
		else{
		//NO
			CDefenseStruct *_enemy = CBaseUnit::GetClosestGate();
			//Any structures in range (And paths)?
			//Check the waypoint for barricade/turret that's in range
		//	_enemy = [what's at the spot]
			if(_enemy != NULL)
			{
			//YES
				//Attack it
				SPosition onePos;
				onePos.m_nPosX = (_enemy->GetPosX());
				onePos.m_nPosY = (_enemy->GetPosY());
				SPosition twoPos;
				twoPos.m_nPosX = (this->GetPosX());
				twoPos.m_nPosY = (this->GetPosY());
			
				bool bInFront = false;
				if( GetTeam() == 1 ) // it's to the right
				{
					if(_enemy->GetPosX() > GetPosX()) bInFront = true;
				}
				else
				{
					if(_enemy->GetPosX()+_enemy->GetWidth() < GetPosX()+GetWidth()) bInFront = true;
				}

				if(DistanceBetween(onePos,twoPos) < GetRange() && bInFront)
					SetTarget(_enemy);
			}
		}
	CBaseUnit::Update(dT);
}
/////////////////////////////////////////////////////////////////////////////
// Function		: Render
// Notes		: Renders the rocket unit to the screen
/////////////////////////////////////////////////////////////////////////////
void CRocketeer::Render(){
	CBaseUnit::Render();
}